package com.tanhua.listener;

import com.tanhua.api.mongo.PublishApi;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.template.HuaWeiUGCTemplate;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RocketMQMessageListener(topic = "tanhua-publish",consumerGroup = "tanhua-publish-group")
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;
    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;
    @Override
    public void onMessage(String publishId) {
        System.out.println(publishId);
        //1 根据id查询这条动态
        Publish publish = publishApi.findByIdtoPublish(publishId);
        //2 对这条动态的文本做审核
        boolean check1 = huaWeiUGCTemplate.textContentCheck(publish.getTextContent());
        //3 对这条动态的图片做审核
        List<String> list = publish.getMedias();
        boolean check2 = huaWeiUGCTemplate.imageContentCheck(list.toArray(new String[]{}));
        //4 设置状态 修改数据库
        Integer state=2; //审核未通过

        if(check1 && check2){
            state=1; //审核通过
        }

        //修改状态
        publishApi.updateState(publishId,state);

        System.out.println("审核已完成...");
    }
}
